#include "ObjComp.h"
#include "Core.h"

///************************************************************************/
/// Dimensions
///************************************************************************/

Vec Dimensions::GetCenter()
{
	if (Circular) return Owner->Pos;
	return Owner->Pos+Size/2;
}

bool Dimensions::IntersectsSquare(const Entity& other)
{
	Dimensions* otherdim = other.GetComponent<Dimensions>();
	if (!otherdim)
		return false;
	Vec P2 = Owner->Pos+Size;
	Vec otherP2 = other.Pos+otherdim->Size;
	return (P2.X > other.Pos.X)&&(Owner->Pos.X < otherP2.X)&&(P2.Y > other.Pos.Y)&&(Owner->Pos.Y < otherP2.Y);
}

bool Dimensions::IntersectPoint(const Vec& pt)
{
	if (Circular)
	{
		return VecInCircle(Owner->Pos, Radius, pt);
	}
	else
	{
		Vec P2 = Owner->Pos+Size;
		return (P2.X > pt.X) && (Owner->Pos.X <= pt.X) && (P2.Y > pt.Y) && (Owner->Pos.Y <= pt.Y);
	}
}

bool Dimensions::IntersectsRadius(const Entity& other)
{
	Dimensions* otherdim = other.GetComponent<Dimensions>();
	if (!otherdim)
		return false;
	return VecInCircle(Owner->Pos, Size.X+otherdim->Radius, other.Pos);
}

///************************************************************************/
/// Graphics
///************************************************************************/

void Graphics::Render(const Vec& offset)
{
	float rot = 0;

	Rotation* rc = Owner->GetComponent<Rotation>();
	if (rc) rot = rc->Rot;

	if (Img && ((1U<<CurrentRenderPass) & RenderInPasses))
		DrawPic(Img, Owner->Pos.X, Owner->Pos.Y, rot, BlendColor);
}

///************************************************************************/
/// Dynamics
///************************************************************************/

void Dynamics::Update(float dt)
{
	Vel = Vel - Vel * Friction * dt;
	Owner->Pos += Vel * dt;
}

///************************************************************************/
/// ParticleEmitter
///************************************************************************/

void ParticleEmitter::Update(float dt)
{
	if (PPS == 0)
		return;

	float oopps = 1.0f/PPS;
	mAccum += dt;
	while (mAccum >= oopps)
	{
		mAccum -= oopps;
		AddParticle(Owner->Pos, &Definition)->Age(mAccum);
	}
}